/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fbv.tcc.gui;

import fbv.tcc.chart.GraficoRadiacao;
import fbv.tcc.classes.PontoRadiacao;
import fbv.tcc.utils.Utils;
import fbv.tcc.xls.XlsReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import jxl.read.biff.BiffException;

/**
 *
 * @author Casa
 */
public class Programa extends javax.swing.JFrame {

    public ArrayList<PontoRadiacao> pontosPlanilhaList = new ArrayList<PontoRadiacao>();
    
    class XLSFilter extends FileFilter {

        @Override
        public boolean accept(File file) {
            if (file.isDirectory()) {
                return true;
            }

            String extension = Utils.getExtension(file);
            if (extension != null) {
                if (extension.equals(Utils.xls) || extension.equals(Utils.xlsx)) {
                    return true;
                } else {
                    return false;
                }
            }

            return false;
        }

        @Override
        public String getDescription() {
            // Esta descrição será exibida na caixa de diálogo,
            // hard-coded = ugly, deve ser feito via I18N
            return "Planihas (*.xls, *.xlsx)";
        }
    }

    /**
     * Creates new form main
     */
    public Programa() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar = new javax.swing.JToolBar();
        btnCarregarPlanilhas = new javax.swing.JButton();
        btnGerarGrafico = new javax.swing.JButton();
        btnSobre = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setName("main"); // NOI18N
        setPreferredSize(new java.awt.Dimension(1024, 800));

        jToolBar.setFloatable(false);
        jToolBar.setRollover(true);
        jToolBar.setAlignmentY(0.5F);
        jToolBar.setAutoscrolls(true);
        jToolBar.setBorderPainted(false);

        btnCarregarPlanilhas.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fbv/tcc/imgs/excelIcon.png"))); // NOI18N
        btnCarregarPlanilhas.setText("Carregar Planilhas");
        btnCarregarPlanilhas.setToolTipText("Carregar Planilhas");
        btnCarregarPlanilhas.setBorderPainted(false);
        btnCarregarPlanilhas.setFocusable(false);
        btnCarregarPlanilhas.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnCarregarPlanilhas.setIconTextGap(1);
        btnCarregarPlanilhas.setMargin(new java.awt.Insets(2, 5, 2, 5));
        btnCarregarPlanilhas.setMaximumSize(new java.awt.Dimension(120, 75));
        btnCarregarPlanilhas.setName(""); // NOI18N
        btnCarregarPlanilhas.setPreferredSize(new java.awt.Dimension(120, 73));
        btnCarregarPlanilhas.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnCarregarPlanilhas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCarregarPlanilhasActionPerformed(evt);
            }
        });
        jToolBar.add(btnCarregarPlanilhas);

        btnGerarGrafico.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fbv/tcc/imgs/graphIcon.png"))); // NOI18N
        btnGerarGrafico.setText("Gerar Gráficos");
        btnGerarGrafico.setBorderPainted(false);
        btnGerarGrafico.setFocusable(false);
        btnGerarGrafico.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnGerarGrafico.setMaximumSize(new java.awt.Dimension(120, 75));
        btnGerarGrafico.setPreferredSize(new java.awt.Dimension(120, 73));
        btnGerarGrafico.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnGerarGrafico.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGerarGraficoActionPerformed(evt);
            }
        });
        jToolBar.add(btnGerarGrafico);

        btnSobre.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fbv/tcc/imgs/pigIcon.png"))); // NOI18N
        btnSobre.setText("Sobre");
        btnSobre.setBorderPainted(false);
        btnSobre.setFocusable(false);
        btnSobre.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSobre.setIconTextGap(1);
        btnSobre.setMaximumSize(new java.awt.Dimension(120, 75));
        btnSobre.setPreferredSize(new java.awt.Dimension(120, 75));
        btnSobre.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSobre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSobreActionPerformed(evt);
            }
        });
        jToolBar.add(btnSobre);

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/fbv/tcc/imgs/closeIcon.png"))); // NOI18N
        btnSair.setText("Sair");
        btnSair.setToolTipText("Fechar programa");
        btnSair.setBorderPainted(false);
        btnSair.setFocusable(false);
        btnSair.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnSair.setIconTextGap(1);
        btnSair.setMaximumSize(new java.awt.Dimension(120, 75));
        btnSair.setMinimumSize(new java.awt.Dimension(120, 75));
        btnSair.setPreferredSize(new java.awt.Dimension(120, 75));
        btnSair.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });
        jToolBar.add(btnSair);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar, javax.swing.GroupLayout.DEFAULT_SIZE, 1024, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jToolBar, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        getAccessibleContext().setAccessibleName("PIG 2");
        getAccessibleContext().setAccessibleDescription("PIG 2");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCarregarPlanilhasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCarregarPlanilhasActionPerformed

        File caminhoPlanilha = new File("C:\\Users\\Victor\\Dropbox\\FBV\\TCC\\Pasta sem titulo");
        
        //GUI
        JFileChooser jFileChooserPlanilhas = new JFileChooser();
        jFileChooserPlanilhas.setAcceptAllFileFilterUsed(false);
        jFileChooserPlanilhas.setMultiSelectionEnabled(true);              
        jFileChooserPlanilhas.setCurrentDirectory(caminhoPlanilha);    
        jFileChooserPlanilhas.addChoosableFileFilter(new XLSFilter());
        int returnVal = jFileChooserPlanilhas.showOpenDialog(this);

        //NEGOCIO

        
        //PontoRadiacaoCompare pontoCompare = new PontoRadiacaoCompare();
        XlsReader xls = new XlsReader();


        if ((returnVal == JFileChooser.APPROVE_OPTION)) {
            File[] sheets = jFileChooserPlanilhas.getSelectedFiles();
            for (int i = 0; i < sheets.length; i++) {
                try {                  
                    pontosPlanilhaList.addAll(xls.loadSheetFile(sheets[i].getPath()));

                } catch (BiffException ex) {
                    Logger.getLogger(Programa.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(Programa.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
 
        } else {
            System.out.println("File access cancelled by user.");
        }
    }//GEN-LAST:event_btnCarregarPlanilhasActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnSobreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSobreActionPerformed
        Sobre janela = new Sobre();
        janela.setVisible(true);
    }//GEN-LAST:event_btnSobreActionPerformed

    private void btnGerarGraficoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGerarGraficoActionPerformed
        
       GraficoRadiacao g = new GraficoRadiacao();
       g.geraGraficos(pontosPlanilhaList);
                     
    }//GEN-LAST:event_btnGerarGraficoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows XP".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Programa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Programa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Programa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Programa.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Programa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCarregarPlanilhas;
    private javax.swing.JButton btnGerarGrafico;
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSobre;
    private javax.swing.JToolBar jToolBar;
    // End of variables declaration//GEN-END:variables
}
